package com.sskj.zixun.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.just.agentweb.AgentWeb;
import com.sskj.zixun.R;
import com.sskj.zixun.R2;
import com.sskj.zixun.bean.PlatFormInfoBean;
import com.sskj.zixun.presenter.ZixunDetailActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.LanguageUtil;
import com.sskj.lib.base.BaseActivity;

import java.util.Locale;

import butterknife.BindView;


public class ZixunDetailActivity extends BaseActivity<ZixunDetailActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivBack)
    ImageView ivBack;
    @BindView(R2.id.webView)
    WebView webView;
    @BindView(R2.id.ll_web)
    LinearLayout llWeb;
    private int id;
    private AgentWeb mAgentWeb;

    @Override
    protected int getLayoutResId() {
        return R.layout.zixun_activity_zixun_detail;
    }

    @Override
    public ZixunDetailActivityPresenter getPresenter() {
        return new ZixunDetailActivityPresenter();
    }

    @Override
    protected void initView() {
        id = getIntent().getIntExtra("id", 0);
        if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(LanguageUtil.getAppLocale(this).getLanguage())) {
            tvTitle.setText(App.INSTANCE.getString(R.string.zixunZixunDetailActivity1));
        } else {
            tvTitle.setText("Information details");
        }
        initWeb();
    }

    private void initWeb() {

        ((ViewGroup) (webView.getParent())).removeView(webView);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        webView.setLayerType(View.LAYER_TYPE_NONE, null);
        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(llWeb, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(Color.TRANSPARENT)// 使用默认进度条
                .setWebView(webView)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //注意这里开启 strict 模式 ， 设备低于 4.2 情况下回把注入的 Js 全部清空掉 ， 这里推荐使用 onJsPrompt 通信
                .createAgentWeb()
                .ready()
                .go("");
    }

    @Override
    protected void initData() {
        mPresenter.getZixunDetail(id);
    }

    public static void start(Context context, int id) {
        Intent intent = new Intent(context, ZixunDetailActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    public void setDetailText(PlatFormInfoBean.InfoBean infoBean) {
        String content = infoBean.getDetails().replaceAll("<span.*?</span><span.*?</span>", "");
        content = "<font style=\"color:white;font-size:24px;margin-top:20px\">" + infoBean.getTitle() + "</font><br>\n" +
                "<p size=\"3\" style=\"text-align:right;color:#bbbbbb\">" + infoBean.getCreateTime() + "</p><br>"
                +"<hr size='0.1px',style=\"background-color:#7591b4\"></hr>"
                + "<div style=\"margin-top:1px;color:#ffffff\">" + content + "</div" +
                ">";
        content = "<body style=\"background:#141724;margin:0;padding-left:15px;padding-right:15px\">" + content + "</body" +
                ">";
        mAgentWeb.getUrlLoader().loadData(content, "text/html; charset=UTF-8", null);
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.i("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
//        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
        return true;
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }


}
